import request from './request'

//登录
const userLogin = function (user = {}) {
    return request.post('/login', user, {
        isPublic: true
    })
}
//home左侧菜单
const getLeftMenu = function () {
    return request.get('/menus')
}
//user页面
const getUserList = function (params = {}) {
    return request.get('/users', { params })
}
const addNewUser = function (user = {}) {
    return request.post('/users', user)
}
const removeUserById = function (id = 0) {
    return request.delete(`/users/${id}`)
}
const updateUserStateByUid = function (uid = 0, type = false) {
    return request.put(`/users/${uid}/state/${type}`)
}
const updateUserById = function (id = 0, uForm = {}) {
    return request.put(`/users/${id}`, uForm)
}
//权限页面
const getAuthList = function(){
    return request.get('/rights/list')
}
const getRolesList = function(){
    return request.get('/roles')
}
const addRoles = function(roles={}){
    return request.post('/roles',roles)
}
const deleteRolesById = function(id){
    return request.delete(`roles/${id}`)
}
const modifyRoles = function(id,rolesForm={}){
    return request.put(`/roles/${id}`, rolesForm)
}
//商品页面
const getGoodsList = function(params = {}){
    return request.get('/goods',{params})
}
const deleteGoodsByGid = function(gid=0){
    return request.delete(`/goods/${gid}`)
}
const getCategories = function(params={}){
    return request.get("/categories",{
        params
    })
}
const addCategories = function(cate={}){
    return request.post('/categories', cate)
}
const modifyCategoriesByCid = function(cat_id=0,cateForm={}){
    return request.put(`/categories/${cat_id}`, cateForm)
}
const deleteCategoriesByCid = function(id=0){
    return request.delete(`/categories/${id}`)
}
//订单页面
const getOrderList = function(params = {}){
    return request.get('/orders',{params})
}
const getKuaidiList = function(id=0){
    return request.get(`/kuaidi/${id}`)
}
const getOrderDetails = function(orderId=0){
    return request.get(`/orders/${orderId}`)
}
const getGoodsDetails = function(goods_id=0){
    return request.get(`/goods/${goods_id}`)
}
const modifyAddressById = function(id,newAddress){
    return request.put(`orders/${id}/address`,newAddress)
}
const api = {
    getLeftMenu,
    userLogin,getUserList,addNewUser,removeUserById,updateUserStateByUid,updateUserById,
    getAuthList,getRolesList,deleteRolesById,addRoles,modifyRoles,
    getGoodsList,deleteGoodsByGid,
    getCategories,
    getOrderList,getOrderDetails,modifyAddressById,
    getKuaidiList,
    getGoodsDetails,
    addCategories,modifyCategoriesByCid,deleteCategoriesByCid
}
export default {
    ...api,
    install(Vue) {
        Vue.prototype.$api = api
    }
}